package com.ruoyi.consumer.controller.wallet;

import ch.qos.logback.classic.Logger;
import com.github.pagehelper.PageHelper;
import com.ijpay.core.kit.WxPayKit;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.consumer.domain.*;
import com.ruoyi.consumer.service.impl.*;
import com.ruoyi.consumer.utils.JsonUtils;
import com.ruoyi.consumer.utils.StreamUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;


/**
 * @author: lizhouwei
 * @ date : 2023/10/29
 * @description:
 */
@Api(tags = "钱包相关")
@RestController
@RequestMapping("/api/wallet")
public class PersonWalletController {

    private static final Logger logger = (Logger) LoggerFactory.getLogger(JsonUtils.class);

    @Autowired
    private WalletService walletService;

    @Autowired
    private CUnitPriceServiceImpl cUnitPriceService;

    @Autowired
    private CWalletPersonServiceImpl cWalletPersonService;

    @Autowired
    private CWalletCompanyServiceImpl cWalletCompanyService;

    @Autowired
    private CDealPersonServiceImpl cDealPersonService;

    @Autowired
    private CDealCompanyServiceImpl cDealCompanyService;

    //接收微信支付回调
    @ApiOperation("接收微信支付回调")
    @PostMapping("/recharge-person-wallet-callback")
    public void rechargePersonWalletCallback(HttpServletRequest request, HttpServletResponse response) throws Exception {
        ServletInputStream inputStream = request.getInputStream();
        String notifyXml = StreamUtils.inputStream2String(inputStream, "utf-8");
        if (!notifyXml.isEmpty()) {
            walletService.payNotice(notifyXml);
            walletService.responseToWeixin(response, notifyXml);
        } else {
            walletService.responseToWeixin(response, "");
        }
        Map<String, String> stringStringMap = WxPayKit.xmlToMap(notifyXml);
        logger.info("微信支付回调响应");
        logger.info("是否成功:" + stringStringMap.get("out_trade_no"));
        logger.info("微信支付回调成功");
    }

    //个人钱包尿素余量
    @ApiOperation("个人钱包尿素余量")
    @GetMapping("/wallet-urea")
    public AjaxResult walletUrea(Integer userId) {
        AjaxResult ajaxResult = new AjaxResult();
        CWalletPerson cWalletPerson = cWalletPersonService.selectCWalletPersonByUserId(userId);
        List<CUnitPrice> cUnitPrices = cUnitPriceService.selectCUnitPriceList(new CUnitPrice());
        ajaxResult.put("walletFree", cWalletPerson.getWalletFree());
        ajaxResult.put("walletVip", cUnitPrices.get(0).getUnitVip());
        ajaxResult.put("walletTemporary", cUnitPrices.get(0).getUnitTemporary());
        return ajaxResult;
    }

    //公司钱包尿素余量
    @ApiOperation("公司钱包尿素余量")
    @GetMapping("/company-wallet-urea")
    public AjaxResult companyWalletUrea(Integer companyId) {
        AjaxResult ajaxResult = new AjaxResult();
        CWalletCompany cWalletCompany = cWalletCompanyService.selectCWalletCompanyByWalletPersonId(companyId);
        ajaxResult.put("walletFree", cWalletCompany.getWalletFree());
        return ajaxResult;
    }

    //个人钱包流水
    @ApiOperation("个人钱包流水")
    @GetMapping("/wallet-flow")
    public AjaxResult walletFlow(@ApiParam("用户id") Integer userId,
                                 @ApiParam("0:全部 1:充值 2:消费") Integer dealType,
                                 @ApiParam("页码") Integer pageNum,
                                 @ApiParam("页数") Integer pageSize) {
        CDealPerson cDealPerson = new CDealPerson();
        cDealPerson.setUserId(userId);

        List<CDealPerson> cWalletFlows = new ArrayList<>();
        String orderBy="create_time desc";
        PageHelper.startPage(pageNum, pageSize,orderBy);
        if (dealType == 0) {
            cWalletFlows = cDealPersonService.selectCDealPersonList(cDealPerson);
        } else if (dealType == 1) {
            cDealPerson.setDealType(1);
            cWalletFlows = cDealPersonService.selectCDealPersonList(cDealPerson);
        } else if (dealType == 2) {
            cDealPerson.setDealType(2);
            cWalletFlows = cDealPersonService.selectCDealPersonList(cDealPerson);
        } else {
            return AjaxResult.error("参数错误");
        }


        List<AjaxResult> ajaxResults = new ArrayList<>();
        for (CDealPerson cWalletFlow : cWalletFlows) {
            AjaxResult ajaxResult = new AjaxResult();
            if (cWalletFlow.getDealAmount() == 0) {
                continue;
            }
            ajaxResult.put("dealRecordId", cWalletFlow.getDealRecordId());
            ajaxResult.put("dealType", cWalletFlow.getDealType());
            ajaxResult.put("dealAmount", cWalletFlow.getDealAmount());
            ajaxResult.put("dealTime", DateUtils.parseDateToStr("yyyy/MM/dd HH:mm", cWalletFlow.getCreateTime()));
            ajaxResults.add(ajaxResult);
        }
        return AjaxResult.success(ajaxResults);
    }

    //公司钱包流水
    @ApiOperation("公司钱包流水")
    @GetMapping("/company-wallet-flow")
    public AjaxResult companyWalletFlow(@ApiParam("公司id") Integer companyId,
                                        @ApiParam("0:全部 1:充值 2:消费") Integer dealType,
                                        @ApiParam("页码") Integer pageNum,
                                        @ApiParam("页数") Integer pageSize) {
        CDealCompany cDealCompany = new CDealCompany();
        cDealCompany.setCompanyId(companyId);

        List<CDealCompany> cWalletFlows = new ArrayList<>();
        String orderBy="create_time desc";
        PageHelper.startPage(pageNum, pageSize,orderBy);
        if (dealType == 0) {
            cWalletFlows = cDealCompanyService.selectCDealCompanyList(cDealCompany);
        } else if (dealType == 1) {
            cDealCompany.setDealType(1);
            cWalletFlows = cDealCompanyService.selectCDealCompanyList(cDealCompany);
        } else if (dealType == 2) {
            cDealCompany.setDealType(2);
            cWalletFlows = cDealCompanyService.selectCDealCompanyList(cDealCompany);
        } else {
            return AjaxResult.error("参数错误");
        }

        List<AjaxResult> ajaxResults = new ArrayList<>();
        for (CDealCompany cWalletFlow : cWalletFlows) {
            AjaxResult ajaxResult = new AjaxResult();
            if (cWalletFlow.getDealAmount() == 0) {
                continue;
            }
            ajaxResult.put("dealRecordId", cWalletFlow.getDealRecordId());
            ajaxResult.put("dealType", cWalletFlow.getDealType());
            ajaxResult.put("dealAmount", cWalletFlow.getDealAmount());
            ajaxResult.put("dealTime", DateUtils.parseDateToStr("yyyy/MM/dd HH:mm:ss", cWalletFlow.getCreateTime()));
            ajaxResults.add(ajaxResult);
        }

        return AjaxResult.success(ajaxResults);
    }

}
